package com.itcanteen.reggie.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcanteen.reggie.common.R;
import com.itcanteen.reggie.entity.Category;
import com.itcanteen.reggie.service.CategoryService;
import com.itcanteen.reggie.utils.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2022/10/25 15:33
 */

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {


    @Autowired
    CategoryService categoryService;


    /**
     * 作用： 添加类别
     * @param category
     * @return
     */
    @PostMapping
    public R save(@RequestBody Category category, HttpSession session){
        //1. 取出当前登录的用户id
        Long empId = (Long) session.getAttribute("user");

        //2.补全创建人与修改人的id
        category.setCreateUser(empId);
        category.setUpdateUser(empId);
        //3. 调用service保存类别
        categoryService.save(category);

        //4. 返回结果
        return R.success("添加类别成功");
    }


    @RequestMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize){
        //1. 把当前页和页面大小传递给service，得到一个Page对象
        Page<Category> pageResult = categoryService.findByPage(page,pageSize);
        //2. 返回结果
        return R.success(pageResult);
    }

    /**
     * http://localhost:8080/category?id=1397844263642378242
     * 作用： 删除类别
     *
     */
    @DeleteMapping
    public R page(Long ids){
        categoryService.deleteById(ids);
        return R.success("删除类别成功");
    }

    /**
     * URL: http://localhost:8080/category
     * Method: PUT
     *
     */
    @PutMapping
    public R update(@RequestBody  Category category,HttpSession session){
        //1. 取出当前登录的用户id
        Long empId = (Long) session.getAttribute("user");

        //2.补全创建人与修改人的id
        category.setUpdateUser(empId);
        //3. 把category值交给Service
        categoryService.update(category);
        return R.success("修改类别成功");
    }

    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<Category> categories = categoryService.findAll();
        log.info("this is all Category:{}",JSON.toJSONString(categories));

        categories.forEach(i->{
            LocalDateTime createTime = i.getCreateTime();
            LocalDateTime updateTime = i.getUpdateTime();

            log.info("createTime:{}",createTime.toString().replace('T', ' ') );
            log.info("updateTime:{}",updateTime.toString().replace('T', ' '));

            i.setCreateTimeStr(createTime.toString().replace('T', ' '));
            i.setUpdateTimeStr(updateTime.toString().replace('T', ' '));

        });

        String fileName = System.currentTimeMillis()+"导出文件";
        ExcelUtils.exportExcel(categories,"类别","类别sheet",Category.class,fileName,true,response);
    }

    /**
     * 作用：根据type查询列表
     * URL: http://localhost:8080/category/list?type=1
     * Method: get
     *
     */
    @GetMapping("/list")
    public R list(Integer type){
        List<Category> categoryList = categoryService.list(type);
        return R.success(categoryList);
    }

}
